/**
 * 资源加载器
 */
define(['async'],function(async){
	var RSLoader = function(){
		
		//常用标识
		this.rsUrl = false;
		this.data = false;
		this.imgObj = false;
		
		//方法
		/**
		 * 初始化方法
		 */
		this.init = function(rsUrl,callback){
			this.rsUrl = rsUrl;
			async.waterfall([
				//先加载event.json
			    function(callback) {
			    	this.__loadData(function(flag,data){
			    		if(flag){
			    			callback(null,data);
			    			this.data = data;
			    		}
			    		else
			    			callback(data);
			    	});
			    }.bind(this),
			    function(data, callback) {
			    	var images = this.__loadImg(data);
			    	this.imgObj = images;
			    	callback(null);
			    }.bind(this)
			], function (err, result) {
				console.log('资源初始化');
				callback(err);
			}.bind(this));
		}
		
		
		//私有方法
		/**
		 * 加载event.json的方法
		 * @param {Function} callback 回调
		 */
		this.__loadData = function(callback){
			this.__makeRequest(this.rsUrl+"/event.json","",function(flag,data){
				if(flag)
					data = JSON.parse(data);
				callback(flag,data);
			});
		}
		
		/**
		 * 加载image数组的方法
		 */
		this.__loadImg = function(data){
			var record = data.records;
			var len = record.length;
			var i = 0;
			var imgobj = null;
			
			var obj = {
				img : {},
				fileOrPpt : {}
			}
		
			while( i < len ){
            	if(record[i].timestamp < 0)
            		record[i].timestamp = 0;
							
				if(record[i].class == "DRFileRecord"){//如果是图片 ppt file 类资源
					if(record[i].fileType == 1){//如果是图片
						if(!obj.img.hasOwnProperty(record[i].relativeSourcePath)){//并且没有存储上
							imgobj = new Image();
							imgobj.src = this.rsUrl +"/"+ record[i].relativeSourcePath;
							obj.img[record[i].relativeSourcePath] = imgobj;
						}
					}else{//如果是 ppt 或 文件 type == ppt or file
						if(!obj.fileOrPpt.hasOwnProperty(record[i].relativeSourcePath)){
							imgobj = new Image();
							imgobj.src = this.rsUrl + record[i].relativeSourcePath;
							imgobj.pageIndex = record[i].pageIndex;
							obj.fileOrPpt[record[i].relativeSourcePath] = imgobj;
						}
					}
								
				}
				i++;
			}
			return obj;
		}
		
		/**
		 * 对网络请求做简单封装（只能是GET请求）
		 * @param {String} url 网络请求路径
		 * @param {String} data 传送的数据，默认形式：name=value&anothername=othervalue&so=on
		 * @param {Function} callback 回调 参数1：请求成功或失败的标识 参数2：数据
		 */
		this.__makeRequest = function(url,data,callback) {
	        var http_request = false;
	        if (window.XMLHttpRequest) { // Mozilla, Safari,...
	            http_request = new XMLHttpRequest();
	            if (http_request.overrideMimeType) {
	                http_request.overrideMimeType('text/xml');
	            }
	        } else if (window.ActiveXObject) { // IE
	            try {
	                http_request = new ActiveXObject("Msxml2.XMLHTTP");
	            } catch (e) {
	                try {
	                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
	                } catch (e) {}
	            }
	        }
	
	        if (!http_request) {
	            console.error('什么破浏览器，ajax都支持不了！！');
	            callback(false,new Error("浏览器不支持AJAX"));
	            return false;
	        }
	        http_request.onreadystatechange = function(){
	        	if (this.readyState == 4) {
		            if (this.status == 200) {
		                callback(true,this.responseText);
		            } else {
		                callback(false,new Error("AJAX请求失败"));
		            }
		        }
	        };
	        http_request.open('GET', url, true);
	        http_request.send(data);
    	}
	};
	return RSLoader;
});